/**
 * 
 */
package com.googlecode.cswish;

import com.googlecode.cswish.struts.BaseActionSupport;
import com.googlecode.cswish.util.StringUtil;

/**
 * Get the configuration of web project environment
 * 
 * @author Jerry.Feng Date: 2008-4-3
 *
 */
public class Config {	
	
	private final String[] BLANK_STRINGS = new String[] {""};
	
	private String projectName = "*";
	
	/**
	 * the package prefix of the application
	 * such as: com.yourcompany
	 */
	private String[] servicePackages = BLANK_STRINGS;
	private String[] servicePackageSuffixs = BLANK_STRINGS;
	private String[] actionPackages = BLANK_STRINGS;
	private String[] actionPackageSuffixs = BLANK_STRINGS;
	
	/**
	 * the default value is same with 'appPackage'
	 */
	private String[] modelPackages = BLANK_STRINGS;
	private String[] modelPackageSuffixs = BLANK_STRINGS;

	/**
	 * the directory of jsp/ftl/vm file
	 */
	private String viewDir = "/";
	
	/**
	 * the template directory to display default view template
	 * different template has different style, such as facebook style, amazon style etc
	 */
	private String viewTemplate = "/viewTemplate/";
	
	/**
	 * CSS directory
	 */
	private String cssDir = "/css/default/";
	
	/**
	 * Image directory
	 */
	private String imgDir = "/images/default/";
	
	///////////////////////////////// pagination //////////////////////////////
	private int pageSize = 10;	// default value
	
	///////////////////////////////// actionSupport ////////////////////////////////
	// see ActionFactoryBean.
	private Class baseAction = BaseActionSupport.class;
	
	///////////////////////////////// display /////////////////////////////////
	private String dataFormat = "yyyy-MM-dd";
	private String detailDataFormat = "yyyy-MM-dd HH:mm:ss";
	
	///////////////////////////////// security ////////////////////////////////
	/**
	 * enable security check
	 */
	private boolean securityEnable;
	private String securityFreePath = "/$/";
	private String securityforbidPath = "/view/";
	private String securityWarningPage = "/$/login/warning.execute";
	private String securityLoginPage = "/$/login/login.execute";
	// enable to validate user info from windows domain
	private String securityDomain;

	public String getServicePackage() {
		return getServicePackages()[0];
	}
	
	public String[] getServicePackages() {
		return servicePackages;
	}
	public String[] getServicePackageSuffixs() {
		return servicePackageSuffixs;
	}
	
	public String getModelPackage() {
		return getModelPackages()[0];
	}
	
	public String[] getModelPackages() {
		if (modelPackages == BLANK_STRINGS) {
			modelPackages = servicePackages;
		}
		return modelPackages;
	}
	
	public String[] getModelPackageSuffixs() {
		if (modelPackageSuffixs == BLANK_STRINGS) {
			modelPackageSuffixs = new String[getModelPackages().length];
			init(modelPackageSuffixs, "");
		}
		return modelPackageSuffixs;
	}

	public void setModelPackage(String modelPackage) {
		String[] packages = StringUtil.split(modelPackage, ',');
		this.modelPackages = new String[packages.length];
		this.modelPackageSuffixs = new String[packages.length];
		updatePackage(packages, this.modelPackages, this.modelPackageSuffixs);
	}

	public void setServicePackage(String servicePackage) {
		String[] packages = StringUtil.split(servicePackage, ',');
		this.servicePackages = new String[packages.length];
		this.servicePackageSuffixs = new String[packages.length];
		updatePackage(packages, this.servicePackages, this.servicePackageSuffixs);
	}
	
	public void setActionPackage(String actionPackage) {
		String[] packages = StringUtil.split(actionPackage, ',');
		this.actionPackages = new String[packages.length];
		this.actionPackageSuffixs = new String[packages.length];
		updatePackage(packages, this.actionPackages, this.actionPackageSuffixs);
	}
	
	private void init(String[] ss, String suffix) {
		for (int i = 0; i < ss.length; i++) {
			if (ss[i] == null) {
				ss[i] = suffix;
			} else {
				ss[i] = ss[i] + suffix;
			}
		}
	}
	
	public String getActionPackage() {
		return getActionPackages()[0];
	}
	
	public String[] getActionPackages() {
		return actionPackages;
	}
	public String[] getActionPackageSuffixs() {
		return actionPackageSuffixs;
	}
	
	public String getViewDir() {
		return viewDir;
	}

	public void setViewDir(String viewDir) {
		this.viewDir = endWith(viewDir, '/');
	}

	public String getViewTemplate() {
		return viewTemplate;
	}

	public void setViewTemplate(String templateDir) {
		this.viewTemplate = endWith(templateDir, '/');
	}
	
	public String getCssDir() {
		return cssDir;
	}

	public void setCssDir(String cssDir) {
		this.cssDir = endWith(cssDir, '/');
	}
	
	private String endWith(String s, char c) {
		if (s.charAt(s.length() - 1) != c) {
			return s + c;
		} else {
			return s;
		}
	}

	private void updatePackage(String[] ss, String[] packageNames, String[] suffixs) {
		for (int i = 0; i < ss.length; i++) {
			String s = ss[i];
			int index = s.indexOf("{0}");
			String suffix;
			String packageName;
			if (index > 0) {
				packageName = s.substring(0, index);
				suffix = s.substring(index + 3);
			} else {
				packageName = s;
				suffix = "";
			}
			
			String format = packageName;
			if (index < 0) {
				// add '.' automatically if the package is a simple format -- don't include '{0}'
				if (packageName.charAt(packageName.length() - 1) != '.') {
					format = packageName + '.';
				}
			}
			
			packageNames[i] = format;
			suffixs[i] = suffix;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isSecurityEnable() {
		return securityEnable;
	}

	public void setSecurityEnable(boolean securityEnable) {
		this.securityEnable = securityEnable;
	}

	public String getSecurityFreePath() {
		return securityFreePath;
	}

	public void setSecurityFreePath(String securityFreePath) {
		this.securityFreePath = securityFreePath;
	}

	public String getSecurityforbidPath() {
		return securityforbidPath;
	}

	public void setSecurityforbidPath(String securityforbidPath) {
		this.securityforbidPath = securityforbidPath;
	}

	public String getSecurityWarningPage() {
		return securityWarningPage;
	}

	public void setSecurityWarningPage(String securityWarningPage) {
		this.securityWarningPage = securityWarningPage;
	}

	public String getSecurityLoginPage() {
		return securityLoginPage;
	}

	public void setSecurityLoginPage(String securityLoginPage) {
		this.securityLoginPage = securityLoginPage;
	}
	
	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	
	public Class getBaseAction() {
		return baseAction;
	}

	public void setBaseAction(Class baseAction) {
		this.baseAction = baseAction;
	}

	public String getDataFormat() {
		return dataFormat;
	}

	public void setDataFormat(String dataFormat) {
		this.dataFormat = dataFormat;
	}
	
	public String getDetailDataFormat() {
		return detailDataFormat;
	}

	public void setDetailDataFormat(String detailDataFormat) {
		this.detailDataFormat = detailDataFormat;
	}

	public String getImgDir() {
		return imgDir;
	}

	public void setImgDir(String imgDir) {
		this.imgDir = imgDir;
	}

	public String getSecurityDomain() {
		return securityDomain;
	}

	public void setSecurityDomain(String securityDomain) {
		this.securityDomain = securityDomain;
	}

	@Override
	public String toString() {
		return "actionPackage=" + getActionPackage() + '\n'
				+ "servicePackage=" + getServicePackage() + '\n'
				+ "modelPackage=" + getModelPackage() + '\n'
				+ "cssDir=" + getCssDir() + '\n'
				+ "imgDir=" + getImgDir() + '\n'
				+ "viewDir=" + getViewDir() + '\n'
				+ "viewTemplate=" + getViewTemplate() + '\n'
				
				+ "securityEnable=" + isSecurityEnable() + '\n'
				+ "securityFreePath=" + getSecurityFreePath() + '\n'
				+ "securityforbidPath=" + getSecurityforbidPath() + '\n'
				+ "securityLoginPage=" + getSecurityLoginPage() + '\n'
				+ "securityLoginPage=" + getSecurityLoginPage() + '\n'
		;
	}
}

